package com.ronri.ams.task;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.application.InitApplication;
import com.ronri.adapter.CommonAdapter;
import com.ronri.ams.SystemEnvironmental;
import com.ronri.ams.bean.breedQuotaListBean;
import com.ronri.ams.bean.breedSampleListBean;
import com.ronri.ams.sample.abstract_activity;
import com.ronri.common.UrlEncode;
import com.ronri.common.WaitDialog;
import com.ronri.db.RemoteDataService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * Created by Administrator on 2017/5/31.
 */

public class activity_task_detect_detail extends abstract_activity {

    // 列表数据
    private List<Map<String, Object>> mItems = new ArrayList<Map<String, Object>>();

    // 任务信息
    private breedQuotaListBean mTaskListBean = null;

    // 返回按钮
    private RelativeLayout btnBack = null;

    // 列表视图
    private ListView mListView = null;

    private Context mContext = null;

    private int mProject_id = -1;
    private int mTask_id = -1;
    private int mNature = 1;

    private String mDeptName = "";
    private String mEndDate = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.task_detailed_list);
        Log.i("aaa","activity_task_detect_detail");
        btnBack = (RelativeLayout) findViewById(R.id.btnBack);

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        // CreateDemoData();

        TextView textView = (TextView) findViewById(R.id.title_text_tv);
        textView.setText("任务详情");

        Bundle bundle = getIntent().getExtras();

        if (bundle != null) {
            mProject_id = bundle.getInt("project_id");
            mTask_id = bundle.getInt("task_id");
        }

        mContext = this;

        mListView = (ListView) findViewById(R.id.listTaskDetail);

        getRemoteData();
    }

    /****
     * 获取远程数据
     */
    public void getRemoteData() {

        RemoteDataService remoteDataService = new RemoteDataService();

        // 获取远程地址
        String szRemoteAddress = InitApplication.SystemConfig.getBase().RemoteAddress(
                InitApplication.SystemConfig.getModule().getBreedQuotaList().getAddress());

        Map<String, Object> mParam = new HashMap<String, Object>();

        try {
            mParam.put("addFrom", 1);
            mParam.put("addMark", "android");
            mParam.put("page", 1);
            mParam.put("pageSize", 100);
            mParam.put("project_id", mProject_id);
            mParam.put("task_id", mTask_id);

        } catch (Exception ex) {

        }

        // 获取远程数据
        remoteDataService.getRemoteData(szRemoteAddress,
                UrlEncode.getUrlParamsByMap(mParam, false),
                InitApplication.SystemConfig.getModule().getTdetail().getMethod(),
                new RemoteDataService.OnRemoteDataEvent() {
                    // 返回结果
                    @Override
                    public void OnResultEvent(String szString) {

                        if (szString.isEmpty()) {
                            ShowNoData(true);
                            return;
                        }

                        try {
                            mTaskListBean = JSON.parseObject(szString, breedQuotaListBean.class);
                            if(mTaskListBean.getData().getRows().size()>0){
                                if (mTaskListBean.isSuccess()) {
                                    if (mListView != null) {
                                        // 获取List视图
                                        mListView = (ListView) findViewById(R.id.listTaskDetail);

                                        List<Map<String, Object>> list = new ArrayList<>();

                                        for (breedQuotaListBean.DataBean.RowsBean item : mTaskListBean.getData().getRows()) {
                                            Map<String, Object> ii = new HashMap<>();
                                            ii.put("breed_name", item.getBreed_name());
                                            ii.put("lab_all_code", item.getLab_all_code());
                                            ii.put("status_name", item.getStatus_name());
                                            ii.put("waste_name", item.getWaste_name());
                                            ii.put("section_name", item.getSection_name());
                                            ii.put("sample_area_name", item.getSample_area_name());
                                            ii.put("inspectd_unit_name", item.getInspectd_unit_name());
                                            list.add(ii);
                                        }

                                        String[] field = {"breed_name", "lab_all_code", "status_name", "waste_name", "section_name", "sample_area_name", "inspectd_unit_name"};
                                        int[] fid = {R.id.breed_name, R.id.lab_all_code, R.id.status_name, R.id.waste_name, R.id.section_name, R.id.sample_area_name, R.id.inspectd_unit_name};

                                        mListView.setAdapter(new SimpleAdapter(mContext, list, R.layout.v_sample_detect, field, fid));
                                    }
                                }
                            }else{
                                ShowNoData(true);
                            }
                        } catch (Exception ex) {

                        }
                    }

                    // 状态控制，是否显示加载过程
                    @Override
                    public void OnRuningState(int nStatus) {
                        if (nStatus == SystemEnvironmental.StartGetDataFromServer) {
                            WaitDialog.ShowDialog(mContext, "请稍后...");
                        } else {
                            WaitDialog.HideDialog();
                        }
                    }

                    @Override
                    public String OnGetCustomParam() {
                        return null;
                    }
                }
        );
    }

    class taskAdapter extends CommonAdapter<breedSampleListBean.DataBean.RowsBean> {

        /****
         * 构造函数
         * @param context
         * @param layoutId
         * @param mDatas
         */
        public taskAdapter(Context context, int layoutId, List<breedSampleListBean.DataBean.RowsBean> mDatas) {
            super(context, layoutId, mDatas);
        }

        @Override
        public void addDataToView(breedSampleListBean.DataBean.RowsBean rowsBean, View o) {

        }

        @Override
        public String FormatData(String szField, String szValue) {
            return null;
        }
    }

    /***
     * 显示未找到数据
     * @param bShow
     */
    private void ShowNoData(boolean bShow) {
        RelativeLayout rr = (RelativeLayout) findViewById(R.id.vNoData);
        if (rr != null) {
            if (bShow == true) {
                rr.setVisibility(View.VISIBLE);
            } else {
                rr.setVisibility(View.GONE);
            }
        }
    }
}
